<?php
use Swoole\Http\Request;
use App\Services\WebSocket\WebSocket;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cache;
use App\Services\Websocket\Facades\Websocket as WebsocketProxy;

WebsocketProxy::on('connect',function(WebSocket $webSocket,Request $request){
    $webSocket->setSender($request->fd);
    $webSocket->emit('connect','welcome');
});

WebsocketProxy::on('disconnect',function(Websocket $webSocket,$data){
    roomout($webSocket,$data);
});

WebsocketProxy::on('login', function (WebSocket $websocket, $data) {
    if (!empty($data->token) && ($user = \App\User::where('api_token', $data->token)->first())) {
        $websocket->loginUsing($user);
        // 获取未读消息
        $rooms = [];
        foreach (\App\Count::$ROOMLIST as $roomid) {
            // 循环所有房间
            $result = \App\Count::where('user_id', $user->id)->where('room_id', $roomid)->first();
            $roomid = 'room' . $roomid;
            if ($result) {
                $rooms[$roomid] = $result->count;
            } else {
                $rooms[$roomid] = 0;
            }
        }
        $websocket->toUser($user)->emit('count', $rooms);
    } else {
        $websocket->emit('login', '登录后才能进入聊天室');
    }
});



WebsocketProxy::on('room',function(WebSocket $webSocket,$data){
    if (!empty($data->api_token) && ($user =\App\User::where('api_token',$data->api_token)->first())){
        if (empty($data->roomid)){
            return;
        }
        $roomId =$data->roomid;
        \App\Traits\RedisOperation::hset(,'so')

        //Redis::command('hset',['socket_id',$user->id,$webSocket->getSender()]);
        // 将该房间下用户未读消息清零
//        $count = \App\Count::where('user_id', $user->id)->where('room_id', $roomId)->first();
//        $count->count = 0;
//        $count->save();
        // 将用户加入指定房间
        $room = \App\Count::$ROOMLIST[$roomId];
        $webSocket->join($room);
        Log::info($user->name .'进入房间:'.$room);
        // 更新在线用户信息
        $roomUsersKey = 'online_users_' . $room;
        $onelineUsers = Cache::get($roomUsersKey);
        $user->src = $user->avatar;
        if ($onelineUsers) {
            $onelineUsers[$user->id] = $user;
            Cache::forever($roomUsersKey, $onelineUsers);
        } else {
            $onelineUsers = [
                $user->id => $user
            ];
            Cache::forever($roomUsersKey, $onelineUsers);
        }
        // 广播消息给房间内所有用户
        Log::info($onelineUsers);
        $webSocket->to($room)->emit('room', $onelineUsers);


    }else {
        $webSocket->emit('login', '登录后才能进入聊天室');
    }
});

WebsocketProxy::on('roomout',function(WebSocket $websocket, $data){
    roomout($websocket,$data);
});



function roomout(WebSocket $websocket, $data) {
    if (!empty($data->api_token) && ($user =\App\User::where('api_token',$data->api_token)->first())){
        if (empty($data->roomid)){
            return;
        }
        $roomId = $data->roomid;
        $room = \App\Count::$ROOMLIST[$roomId];
        $roomUsersKey = 'online_users_'.$room;
        // 更新在线用户信息
        $onelineUsers = Cache::get($roomUsersKey);
        if (!empty($onelineUsers[$user->id])) {
            unset($onelineUsers[$user->id]);
            Cache::forever($roomUsersKey, $onelineUsers);
        }
        $websocket->to($room)->emit('roomout', $onelineUsers);
        Log::info($user->name . '退出房间: ' . $room);
        $websocket->leave([$room]);
    }else {
        $websocket->emit('login', '登录后才能进入聊天室');
    }
}

?>
